/*******************************************************************************
 *  Copyright (C) 2017 iQIYI Holdings, Inc.
 *  All Rights Reserved.
 *
 *  This file is part of cloud service. Unauthorized copy of this file, via any
 *  medium is strictly prohibited. Proprietary and Confidential.
 *
 *  Author(s):
 *      Xiaoshuang LU <luxiaoshuang@qiyi.com>
*******************************************************************************/

package iqiyi.solar.example.http.client.okhttp;

import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import iqiyi.solar.client.http.okhttp.OkhttpHTTPClient;
import iqiyi.solar.example.AbstractClient;
import iqiyi.solar.example.CommandOptionParser;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkhttpHTTPClient2 extends AbstractClient {
    private static final Logger LOGGER = LoggerFactory.getLogger(OkhttpHTTPClient2.class);

    private OkhttpHTTPClient solarClient;

    private AtomicBoolean flag = new AtomicBoolean(true);

    public OkhttpHTTPClient2(String serverAddress, int serverPort) {
        super(serverAddress, serverPort);
    }

    @Override
    public int open() throws Exception {
        ApplicationContext applicationContext =
            new ClassPathXmlApplicationContext("spring/OkhttpHTTPClient2.xml");

        this.solarClient = applicationContext.getBean("client", OkhttpHTTPClient.class);

        this.solarClient.open();

        return 0;
    }

    @Override
    public int run() throws Exception {
        OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();

        this.solarClient.attachMetricRegistry(okHttpClientBuilder);

        for (long i = 0; i < 64; ++ i) {
            try {
                OkHttpClient okHttpClient = okHttpClientBuilder.build();

                String url = "http://solar-kafka-kafka-dev001-shgq.qiyi.virtual:8080/solar-example-http-server-tomcat/test/0";

                Request.Builder requestBuilder = new Request.Builder();
                requestBuilder.url(url);

                Request request = requestBuilder.build();

                Response response = okHttpClient.newCall(request).execute();

                if (response.isSuccessful()) {
                    LOGGER.info(response.body().string());
                } else {
                    LOGGER.warn("failed.");
                }

                response.close();
            } catch (Exception e) {
                LOGGER.error("", e);
            }
        }

        return 0;
    }

    @Override
    public int close() throws Exception {
        this.solarClient.close();

        if (! this.flag.get()) {
            throw new Exception();
        }

        return 0;
    }

    public static void main(String[] stringArray) {
        String serverAddress;
        Integer serverPort;
        CommandOptionParser commandOptionParser = new CommandOptionParser(stringArray);

        commandOptionParser.parse();
        serverAddress = commandOptionParser.getAddress();
        serverPort = commandOptionParser.getPort();

        if (serverAddress == null ) {
            serverAddress = "localhost";
        }

        if (serverPort == null) {
            serverPort = 7171;
        }

        AbstractClient client =
            new OkhttpHTTPClient2(
                serverAddress,
                serverPort);

        try {
            client.open();

            client.run();

            client.close();
        } catch (Exception e) {
            LOGGER.error("", e);
        }
    }
}
